import { addIdentity, addUser, getUser, getIdentity, getAuthority, getAuthorityRelation, getViewAuthority, getIdentityView } from '@/service';
import { IAuthority, IIdentity, IUserName, IRelation, IMyAuthority , IIdentityView } from '@/utils/interface';
import { Effect, Reducer } from 'umi';

export interface IndexModelState {
  userList: IUserName[]
  identityList: IIdentity[]
  authorityList: IAuthority[]
  relationList: IRelation[]
  viewAuthority: IMyAuthority[]
  identityViewList:IIdentityView[]
}
export interface IndexModelType {
  namespace: 'people';
  state: IndexModelState;
  effects: {
    setAddUser: Effect;
    getUser: Effect;
    setAddiIdentity: Effect;
    getIdentity: Effect;
    getAuthority: Effect;
    getAuthorityRelation: Effect;
    getViewAuthority: Effect;
    getIdentityView: Effect;

  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  // subscriptions: { setup: Subscription };
}
const PeopleModel: IndexModelType = {
  namespace: 'people',
  state: {
    userList: [],
    identityList: [],
    authorityList: [],
    relationList: [],
    viewAuthority: [],
    identityViewList:[]
  },
  // 异步操作，理解为vuex中的action, async/await理解为generator函数的语法糖
  effects: {
    *setAddUser({ payload }, { call, put }) {
      let result = yield call(addUser, payload)
      return result
    },
    *getUser({ }, { call, put }) {
      let result = yield call(getUser)
      yield put({
        type: 'save',
        payload: { userList: result.data }
      })
    },
    *setAddiIdentity({ payload }, { call, put }) {
      let result = yield call(addIdentity, payload)
      return result
    },
    *getIdentity({ }, { call, put }) {
      let result = yield call(getIdentity)
      yield put({
        type: 'save',
        payload: { identityList: result.data }
      })
    },
    *getAuthority({ }, { call, put }) {
      let result = yield call(getAuthority)
      yield put({
        type: 'save',
        payload: { authorityList: result.data }
      })
    },
    *getAuthorityRelation({ }, { call, put }) {
      let result = yield call(getAuthorityRelation)
      yield put({
        type: 'save',
        payload: { relationList: result.data }
      })
    },
    *getViewAuthority({ }, { call, put }) {
      let result = yield call(getViewAuthority)
      yield put({
        type: 'save',
        payload: { viewAuthority: result.data }
      })
    },
    *getIdentityView({ }, { call, put }) {
      let result = yield call(getIdentityView)
      yield put({
        type: 'save',
        payload: { identityViewList: result.data }
      })
    }
  },
  // 同步操作，理解为vuex中的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    }
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
};
export default PeopleModel

